package com.spdzen.support;

import java.util.LinkedList;
import java.util.List;

import com.github.pagehelper.Page;

/**
 * 分页工具类
 * @author accountwcx@qq.com
 *
 */
public class PageUtils {	
	/**
	 * 校验页面记录的开始索引，如果为null或者小于0，返回{@link #DEFAULT_OFFSET}
	 * @param offset
	 * @return 返回{@link #DEFAULT_OFFSET}或者offset
	 */
	public static int checkOffset(Integer offset) {
		if(offset == null){
			return SimplePage.DEFAULT_OFFSET;
		}
		
		int value = offset.intValue();		
		return value < 0 ? SimplePage.DEFAULT_OFFSET : value;
	}
	
	/**
	 * 校验页面记录数量，如果为null或者小于1，返回{@link #SimplePage.DEFAULT_PAGE_SIZE}
	 * @param limit
	 * @return 返回{@link #DEFAULT_LIMIT}或者limit
	 */
	public static int checkLimit(Integer limit) {
		if(limit == null){
			return SimplePage.DEFAULT_PAGE_SIZE;
		}
		
		int value = limit.intValue();
		return value < 1 ? SimplePage.DEFAULT_PAGE_SIZE : value;
	}
	
	public static int checkPageNum(Integer pageNum) {
		if(pageNum == null) {
			return SimplePage.DEFAULT_PAGE_NUM;
		}
		
		int val = pageNum.intValue();
		return val < 1 ? SimplePage.DEFAULT_PAGE_NUM : val;
	}
	
	public static int checkPageSize(Integer pageSize) {
		if(pageSize == null) {
			return SimplePage.DEFAULT_PAGE_SIZE;
		}
		
		int val = pageSize.intValue();
		return val < 1 ? SimplePage.DEFAULT_PAGE_SIZE : val;
	}
	
	/**
	 * 获取页数，如果limit或者offset小于1，返回页数默认为1。
	 * @param offset 页面记录开始索引
	 * @param limit 页面记录数
	 * @return 1或者计算后的页数
	 */
	public static int getPageNum(Integer offset, Integer limit) {
		int ioffset = checkOffset(offset);
		int ilimit = checkLimit(limit);
		
		if(ioffset < 1 || ilimit < 1){
			return 1;
		}
		
		return ioffset / ilimit + 1;
	}
	
	public static int getPageNum(PageOffsetParam pageParam) {
		return getPageNum(pageParam.getOffset(), pageParam.getLimit());
	}
	
	public static int getOffset(Integer pageNum, Integer pageSize) {
		int ipn = checkPageNum(pageNum);
		int ili = checkPageSize(pageSize);
		
		return (ipn - 1) * ili;
	}
	
	public static int getOffset(PageNumParam pageParam) {
		return getOffset(pageParam.getPageNum(), pageParam.getPageSize());
	}
	
	public static PageOffsetParam toPageOffsetParam(PageNumParam pnparam) {
		PageOffsetParam param = new PageOffsetParam();
		
		int pageNum = checkPageNum(pnparam.getPageNum());
		int pageSize = checkPageSize(pnparam.getPageSize());
		int offset = (pageNum - 1) * pageSize;
		
		param.setOffset(offset);
		param.setLimit(pageSize);
		
		return param;
	}
	
	public static PageNumParam toPageNumParam(PageOffsetParam param) {
		PageNumParam pnparam = new PageNumParam();
		
		pnparam.setPageNum(getPageNum(param));
		pnparam.setPageSize(checkLimit(param.getLimit()));
		
		return pnparam;
	}
	
	public static <T> SimplePage<T> toSimplePage(List<T> list) {
		SimplePage<T> simplePage = new SimplePage<T>();
		
		if(list != null) {
			if(list instanceof Page) {
				Page<T> page = (Page<T>) list;
				
				simplePage.setPageNum(page.getPageNum());
				simplePage.setPageSize(page.getPageSize());
				simplePage.setTotal(page.getTotal());
				
				List<T> rows = new LinkedList<T>();
				for(T item : page){
					rows.add(item);
				}
				
				simplePage.setRows(rows);
			} else {
				simplePage.setPageSize(list.size());
				simplePage.setTotal(list.size());
				simplePage.setRows(list);
			}
		}
		
		return simplePage;
	}
}
